package com.gitee.zycra.union.api.web.controller;

import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.union.api.common.bo.CommonResult;
import com.gitee.zycra.union.api.common.bo.PageQuery;
import com.gitee.zycra.union.api.common.param.AddInterfaceCaseParam;
import com.gitee.zycra.union.api.common.param.AddInterfaceParam;
import com.gitee.zycra.union.api.common.param.PageQueryInterfaceParam;
import com.gitee.zycra.union.api.common.param.UpdateInterfaceCaseParam;
import com.gitee.zycra.union.api.common.param.UpdateInterfaceParam;
import com.gitee.zycra.union.api.common.vo.InterfaceCaseVO;
import com.gitee.zycra.union.api.common.vo.InterfaceFolderVO;
import com.gitee.zycra.union.api.common.vo.InterfaceVO;
import com.gitee.zycra.union.api.service.ApiInterfaceCaseService;
import com.gitee.zycra.union.api.service.ApiInterfaceFolderService;
import com.gitee.zycra.union.api.service.ApiInterfaceService;
import com.gitee.zycra.union.api.web.annotation.AuthCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zycra
 */
@RestController
@RequestMapping("/{groupId}/interface")
public class ApiInterfaceController {

    @Autowired
    private ApiInterfaceFolderService apiInterfaceFolderService;

    @Autowired
    private ApiInterfaceService apiInterfaceService;

    @Autowired
    private ApiInterfaceCaseService apiInterfaceCaseService;

    /**
     * 添加目录
     *
     * @param projectId  项目ID
     * @param folderName 目录名称
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/addFolder")
    public CommonResult<String> addFolder(@RequestParam("projectId") Long projectId, @RequestParam("folderName") String folderName) {
        return apiInterfaceFolderService.addFolder(projectId, folderName);
    }

    /**
     * 更新目录
     *
     * @param folderId   目录ID
     * @param folderName 目录名称
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/updateFolder")
    public CommonResult<String> updateFolder(@RequestParam("folderId") Long folderId, @RequestParam("folderName") String folderName) {
        return apiInterfaceFolderService.updateFolder(folderId, folderName);
    }

    /**
     * 删除目录
     *
     * @param folderId 目录ID
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/deleteFolder")
    public CommonResult<String> deleteFolder(@RequestParam("folderId") Long folderId) {
        return apiInterfaceFolderService.deleteFolder(folderId);
    }

    /**
     * 查询所有目录
     *
     * @param projectId 项目ID
     * @return 目录集合
     */
    @AuthCheck
    @PostMapping("/folderList")
    public CommonResult<List<InterfaceFolderVO>> folderList(@RequestParam("projectId") Long projectId) {
        return apiInterfaceFolderService.selectByProjectId(projectId);
    }

    /**
     * 添加接口
     *
     * @param addInterfaceParam 添加参数
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/addInterface")
    public CommonResult<String> addInterface(@RequestBody @Validated AddInterfaceParam addInterfaceParam) {
        return apiInterfaceService.addInterface(addInterfaceParam);
    }

    /**
     * 更新接口
     *
     * @param updateInterfaceParam 更新参数
     * @return 更新结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/updateInterface")
    public CommonResult<String> updateInterface(@RequestBody @Validated UpdateInterfaceParam updateInterfaceParam) {
        return apiInterfaceService.updateInterface(updateInterfaceParam);
    }

    /**
     * 删除接口
     *
     * @param id 接口ID
     * @return 删除结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/deleteInterface")
    public CommonResult<String> deleteInterface(@RequestParam("id") Long id) {
        return apiInterfaceService.deleteInterface(id);
    }

    /**
     * 查询接口
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    @AuthCheck
    @PostMapping("/interfaceList")
    public CommonResult<PageResult<InterfaceVO>> interfaceList(@RequestBody @Validated PageQuery<PageQueryInterfaceParam> pageQuery) {
        return apiInterfaceService.pageQueryInterface(pageQuery);
    }

    /**
     * 添加期望
     *
     * @param addInterfaceCaseParam 期望参数
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/addInterfaceCase")
    public CommonResult<String> addInterfaceCase(@RequestBody @Validated AddInterfaceCaseParam addInterfaceCaseParam) {
        return apiInterfaceCaseService.addInterfaceCase(addInterfaceCaseParam);
    }

    /**
     * 更新期望
     *
     * @param updateInterfaceCaseParam 期望参数
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/updateInterfaceCase")
    public CommonResult<String> updateInterfaceCase(@RequestBody @Validated UpdateInterfaceCaseParam updateInterfaceCaseParam) {
        return apiInterfaceCaseService.updateInterfaceCase(updateInterfaceCaseParam);
    }

    /**
     * 删除期望
     *
     * @param id 期望ID
     * @return 添加结果
     */
    @AuthCheck({"2", "3"})
    @PostMapping("/deleteInterfaceCase")
    public CommonResult<String> deleteInterfaceCase(@RequestParam("id") Long id) {
        return apiInterfaceCaseService.deleteInterfaceCase(id);
    }

    /**
     * 查询接口期望
     *
     * @param interfaceId 接口ID
     * @return 查询结果
     */
    @AuthCheck
    @PostMapping("/interfaceCaseList")
    public CommonResult<List<InterfaceCaseVO>> interfaceCaseList(@RequestParam("interfaceId") Long interfaceId) {
        return apiInterfaceCaseService.selectByInterfaceId(interfaceId);
    }
}
